/**
 * 
 */
package jvs.vfs.opt.media;

/**
 * 
 * Taken from android.media.MediaScanner.java
 * 
 * @author qiangli
 * 
 */
public final class Genres {
	private static int parseSubstring(String s, int start, int defaultValue) {
		int length = s.length();
		if (start == length) {
			return defaultValue;
		}

		char ch = s.charAt(start++);
		// return defaultValue if we have no integer at all
		if (ch < '0' || ch > '9') {
			return defaultValue;
		}
		int result = ch - '0';
		while (start < length) {
			ch = s.charAt(start++);
			if (ch < '0' || ch > '9') {
				return result;
			}
			result = result * 10 + (ch - '0');
		}
		return result;
	}

	public static String toLabel(String value) {
		// handle numeric genres, which PV sometimes encodes like "(20)"
		if (value.length() > 0) {
			int genreCode = -1;
			char ch = value.charAt(0);
			if (ch == '(') {
				genreCode = parseSubstring(value, 1, -1);
			} else if (ch >= '0' && ch <= '9') {
				genreCode = parseSubstring(value, 0, -1);
			}
			if (genreCode >= 0 && genreCode < ID3_GENRES.length) {
				value = ID3_GENRES[genreCode];
			}
		}
		return value;
	}

	private static final String[] ID3_GENRES = {
			// ID3v1 Genres
			"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
			"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
			"Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial",
			"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
			"Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
			"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House",
			"Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass",
			"Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
			"Instrumental Rock", "Ethnic", "Gothic", "Darkwave",
			"Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance",
			"Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40",
			"Christian Rap", "Pop/Funk", "Jungle", "Native American",
			"Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes",
			"Trailer",
			"Lo-Fi",
			"Tribal",
			"Acid Punk",
			"Acid Jazz",
			"Polka",
			"Retro",
			"Musical",
			"Rock & Roll",
			"Hard Rock",
			// The following genres are Winamp extensions
			"Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion",
			"Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",
			"Gothic Rock", "Progressive Rock", "Psychedelic Rock",
			"Symphonic Rock", "Slow Rock", "Big Band", "Chorus",
			"Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
			"Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
			"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango",
			"Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul",
			"Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella",
			"Euro-House", "Dance Hall" };
}
